package cn.iocoder.yudao.module.dify.controller.admin.translationtaskfile;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dify.controller.admin.translationtaskfile.vo.TranslationTaskFilePageReqVO;
import cn.iocoder.yudao.module.dify.controller.admin.translationtaskfile.vo.TranslationTaskFileRespVO;
import cn.iocoder.yudao.module.dify.controller.admin.translationtaskfile.vo.TranslationTaskFileSaveReqVO;
import cn.iocoder.yudao.module.dify.dal.dataobject.translationtaskfile.TranslationTaskFileDO;
import cn.iocoder.yudao.module.dify.service.translationtaskfile.TranslationTaskFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 -  翻译工作流任务文件")
@RestController
@RequestMapping("/dify/translation-task-file")
@Validated
public class TranslationTaskFileController {

    @Resource
    private TranslationTaskFileService translationTaskFileService;

    @PostMapping("/create")
    @Operation(summary = "创建 翻译工作流任务文件")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-file:create')")
    public CommonResult<Long> createTranslationTaskFile(@Valid @RequestBody TranslationTaskFileSaveReqVO createReqVO) {
        return success(translationTaskFileService.createTranslationTaskFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 翻译工作流任务文件")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-file:update')")
    public CommonResult<Boolean> updateTranslationTaskFile(@Valid @RequestBody TranslationTaskFileSaveReqVO updateReqVO) {
        translationTaskFileService.updateTranslationTaskFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 翻译工作流任务文件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dify:translation-task-file:delete')")
    public CommonResult<Boolean> deleteTranslationTaskFile(@RequestParam("id") Long id) {
        translationTaskFileService.deleteTranslationTaskFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 翻译工作流任务文件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-file:query')")
    public CommonResult<TranslationTaskFileRespVO> getTranslationTaskFile(@RequestParam("id") Long id) {
        TranslationTaskFileDO translationTaskFile = translationTaskFileService.getTranslationTaskFile(id);
        return success(BeanUtils.toBean(translationTaskFile, TranslationTaskFileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得 翻译工作流任务文件分页")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-file:query')")
    public CommonResult<PageResult<TranslationTaskFileRespVO>> getTranslationTaskFilePage(@Valid TranslationTaskFilePageReqVO pageReqVO) {
        PageResult<TranslationTaskFileDO> pageResult = translationTaskFileService.getTranslationTaskFilePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TranslationTaskFileRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出 翻译工作流任务文件 Excel")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-file:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTranslationTaskFileExcel(@Valid TranslationTaskFilePageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TranslationTaskFileDO> list = translationTaskFileService.getTranslationTaskFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, " 翻译工作流任务文件.xls", "数据", TranslationTaskFileRespVO.class,
                BeanUtils.toBean(list, TranslationTaskFileRespVO.class));
    }

    @GetMapping("/getListByTaskId")
    @Parameter(name = "id", description = "任务id", required = true, example = "1024")
    @Operation(summary = "通过任务id获得翻译工作流任务文件列表")
    public CommonResult<List<TranslationTaskFileRespVO>> getTranslationTaskFileListByTaskId(@RequestParam("id") Long id) {
        List<TranslationTaskFileRespVO> pageResult = translationTaskFileService.getTranslationTaskFileListByTaskId(id);
        return success(pageResult);
    }

    @GetMapping(path = "/executeTask")
    @Operation(summary = "执行任务")
    public CommonResult<Boolean> executeTranslateTask(@RequestParam("id") Long id) {
        return CommonResult.success(translationTaskFileService.executeTranslateTask(id));
    }


}